
cc.Class({
    extends: cc.Component,

    properties: {
        meatSprite:[cc.SpriteFrame],
        guoSprite:[cc.SpriteFrame],
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.game = cc.find("Canvas").getComponent('Home');

        this.changeTime = cc.configData.material.meat.time[cc.configData.material.meat.level];
        this.deleteTime = this.changeTime + 3;
        this.duringTime = 0;
        this.startJishi = true;
        this.node.parent.getComponent(cc.Animation).play();
        this.place = false;
        this.placeNode = null;//检测放置的汉堡节点
        this.speedTool();
    },
    startCallFunc(event){
        if(!this.game.singleDrag) return;
        this.game.singleDrag = false;
        if(this.parentInit) return;
        var touches = event.getTouches();
        if(touches.length >= 2) return;
        this.parentInit = this.node.parent;
        this.parentInit.getComponent(cc.Animation).stop();
        this.parentInit.getComponent(cc.Sprite).spriteFrame = this.guoSprite[0]
        this.node.parent = cc.find('Canvas');
        var po = cc.find('Canvas').convertToNodeSpaceAR(this.parentInit.convertToWorldSpaceAR(cc.p(this.node.x,this.node.y)));
        this.node.setPosition(po);
        this.node.zIndex=99;
        this.startJishi= false;
    },
    moveCallFunc(event){
        if(!this.parentInit) return;
        var touches = event.getTouches();
        this.node.x += touches[0].getDelta().x;
        this.node.y += touches[0].getDelta().y;
    },
    endCallFunc(){
        if(!this.parentInit) return;
        if(this.place){
            this.placeNode.getComponent('hamburger').placeMeat();
            this.parentInit.place = true;
            this.node.destroy();
        }else{
            if(!this.parentInit) return;
            this.startJishi= true;
            this.node.parent = this.parentInit;
            this.parentInit.getComponent(cc.Animation).play();
            this.node.setPosition(0,0);
            this.parentInit = null;
        }
        this.game.singleDrag = true;
    },
    drag(){
        this.dragc = true;
        this.node.on('touchstart',this.startCallFunc,this);
        this.node.on('touchmove',this.moveCallFunc,this);
        this.node.on('touchcancel',this.endCallFunc,this)
        this.node.on('touchend',this.endCallFunc,this)
    },
    undrag(){
        this.node.off('touchstart',this.startCallFunc,this);
        this.node.off('touchmove',this.moveCallFunc,this);
        this.node.off('touchcancel',this.endCallFunc,this)
        this.node.off('touchend',this.endCallFunc,this)
    },
    dClick(){
        var  clicked = 1;
        var  clickedTime={
            'timeA':" ",
            'timeB':" "
        };
        this.parentInit = null;
        this.node.on('touchstart',function(event){
            if(clicked==1){
                clickedTime.timeA=new Date().getTime();
                clicked++;
            }
            else if(clicked==2){
                clickedTime.timeB=new Date().getTime();
                if (Math.abs(clickedTime.timeA-clickedTime.timeB)<400){
                    //双击
                    this.deleteMeat();
                    clicked=1;
                }else{
                    clickedTime.timeA=new Date();
                };
            }
        },this);
    },
    onCollisionEnter: function (other, self) {
        this.place = true;
        this.placeNode = other.node;
    },
    onCollisionExit: function (other, self) {
        this.place = false;
    },
    deleteMeat(){
        this.node.parent.getComponent(cc.Animation).stop();
        this.node.parent.getComponent(cc.Sprite).spriteFrame = this.guoSprite[0]
        var Level = cc.configData.material.meat.level
        var price = cc.configData.material.meat.price[Level]//减去肉的价值；
        console.log('meat',price);
        cc.coinWin -= price;
        this.node.parent.place = true;
        this.node.destroy();
    },
    speedTool(){
        cc.find('Canvas').on('speedMeat',function(){
            if(this.startJishi && this.duringTime < this.changeTime - 2){
                this.duringTime = this.changeTime - 2;
            }
        }.bind(this));
    },
    update (dt) {
        if(!this.startJishi) return;
        if(this.duringTime < this.changeTime){
            this.duringTime += cc.speedMeat*dt;
            if(!this.node.children[0].active){
                this.node.children[0].active = true;
            }
        }else if(this.duringTime < this.deleteTime) {
            this.duringTime += dt;
            if(!this.dragc){
                cc.voicePlay.playEffect('foodok');
                this.node.children[0].active = false;
                this.node.children[2].active = true;
                this.node.getComponent(cc.Sprite).spriteFrame = this.meatSprite[1];
                this.node.parent.runAction(cc.sequence(cc.moveBy(0.1,30,0),cc.moveBy(0.1,-50,0),cc.moveBy(0.1,30,0),cc.moveBy(0.1,-15,0),cc.moveBy(0.1,5,0)));
                this.drag();
            }
        }else{  //糊了
            this.startJishi= false;
            if(!this.node.children[1].active){
                this.node.getComponent(cc.Sprite).spriteFrame = this.meatSprite[2]
                this.undrag();
                this.dClick();
                this.node.children[1].active = true;
                this.node.children[0].active = false;
                this.node.children[2].active = false;
                this.node.children[3].active = true;
            }
        }
    },
});
